<template>
  <div>
    <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="部门名称">
        <el-input
          v-model="form.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码">
        <el-input
          v-model="form.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人">
        <el-select
          v-model="form.manager"
          style="width: 80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in employees"
            :key="item.id"
            :value="item.username"
            :label="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍">
        <el-input
          v-model="form.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getEmployees } from '@/api/employees.js'
import { addDepartments, getDepartDetail, editDepartDetail } from '@/api/tree.js'
export default {
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    }
  },
  data() {
    const validName = (rule, value, callback) => {
      console.log(value)
      let nameList = this.originList.filter(it => it.pid === this.id).map(it => it.name)
      console.log(nameList)

      if (this.isEdit) { // 在某个部门上点击编辑的时候，被占用的名字列表=当前部门的兄弟部门-他自己的名字
        const obj = this.originList.find(it => it.id === this.id)
        const pid = obj.pid
        nameList = this.originList.filter(it => it.pid === pid && it.id !== this.id).map(it => it.name)
      }
      if (nameList.includes(value)) {
        callback(new Error(value + '被占用了'))
      } else {
        callback()
      }
    }
    const valiCode = (rule, value, callback) => {
      // const originList=this.originList
      const { originList, isEdit, id } = this
      // console.log('当前校验的值', value)
      // console.log('校验参考的数据是', this.originList)
      let codeList = originList.map(it => it.code)
      // console.log('校验参考的数据是', codeList)

      if (isEdit) { // 对编辑的特殊处理
        codeList = originList.filter(it => it.id !== id).map(it => it.code)
        console.log(codeList)
      }
      codeList.includes(value)
        ? callback(new Error('编码' + value + '已经存在'))
        : callback()
      callback()
    }
    return {
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { validator: valiCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' }
        ]
      },
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      employees: []
    }
  },
  created() {
    this.loadEmployess()
    if (this.isEdit === true) {
      this.loadDepartments()
    }
  },
  methods: {
    async loadDepartments() {
      const res = await getDepartDetail(this.id)
      console.log(res)
      this.form = res.data
    },
    async loadEmployess() {
      const res = await getEmployees()
      console.log(res)
      this.employees = res.data
    },
    hSubmit() {
      this.$refs.deptForm.validate(valid => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    async doAdd() {
      console.log(this.id)
      const res = await addDepartments({ ...this.form, pid: this.id })
      console.log(res)
      if (res.success) {
        this.$emit('success')
      }
    },
    async doEdit() {
      const res = await editDepartDetail({ ...this.form })
      if (res.success) {
        this.$emit('success')
      }
    },
    hCancel() {
      this.$emit('success')
    }
  }
}
</script>

<style>
</style>
